/**********************************************************************/
/*
   Author: Karan Makkar
   Created: Feb  2024
   Updated: Aug 2025, by Youssef Assarssah
   Description: Program Targeting on Shocks and Demographics
   Output: Tables 4, A13
*/
/**********************************************************************/

/*----------------------------------------------------*/
* Section: Setup
/*----------------------------------------------------*/

* include filepaths 
if "$master_run" !="1" include "./Do/SET_FILEPATHS.do"

clear all
set more off
set matsize 11000
set sortseed 123456789

* Log
cap log close
global prefix: display %tdCYND td(`c(current_date)')
log using "$KP_logs/${prefix}_targeting_table.txt", text replace
  
* Varlist
global baseline_vars26 ltot_adj_cons ///
            employed ///
            hospitalized_hh ///
            d_cons26_java
global baseline_vars26_dummy ltot_adj_cons ///
            employed ///
            hospitalized_hh ///
            d_cons26_java_dummy
/*----------------------------------------------------*/
* Prepare Data
/*----------------------------------------------------*/

/*****************
 Combine Datasets
*****************/

* Import March 2020 data
u "${KP_deid_susenas}/Clean/sus_mar20_deid_clean.dta", clear

* Keep obs unique on HH-ID, dob, year, month, gender
drop if mi(renum, year_dob_sus, month_dob_sus, day_dob_sus, gender)
duplicates tag renum year_dob_sus month_dob_sus day_dob_sus gender, gen(dups)
drop if dups !=0

rename renum renumMar
bysort renum (r403 r406c): gen ind_code_hh = ind_code[1]

* Merge in september panel indicator
fmerge 1:1 renumMar year_dob_sus month_dob_sus day_dob_sus gender using "${KP_deid_susenas}/Clean/marchsep_sus_merged.dta", keepusing(get_pk age_sus) assert(1 3)

gen panelobs = _merge ==3
drop _merge

* Merge in leaveout mean shocks
fmerge 1:1 renumMar year_dob_sus month_dob_sus day_dob_sus gender using "${KP_deid_susenas}/Clean/sus_marsep20_leaveoutind26shocks_java.dta", assert(1 3) nogen keepusing(d_employedl d_tot_adj_consl d_l_tot_adj_consl renumMar year_dob_sus month_dob_sus day_dob_sus gender)

* Merge in Admin KP data for appliers
preserve
keep if !mi(anon_id4)
duplicates drop anon_id4, force

merge 1:m anon_id4 using "${KP_deid_susenas}/Clean/sus_mar20_deid_clean_merged.dta", nogen keep(1 3) keepusing(ever_win_5 ever_win_22 batch first_apply_batch batch win_in_batch)

tempfile appliers
sa `appliers'
restore

* Append back appliers data
drop if !mi(anon_id4)

append using `appliers'

/*****************
 Clean
*****************/

****** Cleaning ******

*Merge in java data
rename r101 prov_id
fmerge m:1 prov_id using "${KP_deid_susenas}/Raw/provid_java_crosswalk", assert(3) nogen

* Merge in industry shock data
rename ind_code ind_code26
fmerge m:1 ind_code26 java using "${KP_deid_susenas}/Clean/sus_marsep20_ind26shocks_java.dta", assert(3) nogen

replace ind_code26 = 0 if mi(ind_code26)

* Make receive PKH dummy
replace receive_pkh_year = receive_pkh_year ==1

* Ever Win = 0 if didn't apply
replace ever_win_5 = 0 if mi(ever_win_5)
replace ever_win_22 = 0 if mi(ever_win_22)

* Food insecurity dummy
summ food_insecurity_index_n, d
gen food_insecure = food_insecurity_index_n > 0

* Social Program Dummy
gen socialprogram = (receive_pkh_year == 1 & r403 ==2 & female ==1) | receive_PIP ==1 | ((feb20_BPNT ==1 | jan20_BPNT ==1 | dec19_BPNT ==1 | nov19_BPNT ==1) &  r403 ==2 & female ==1)

* Applier dummy
gen applier = !mi(anon_id4)

* Generate reapply vars
bysort anon_id4 (batch): gen reapplied = _n != _N if applier ==1

* First observation per person/hh
bysort anon_id4: gen num = _n
replace num =1 if mi(anon_id4)

* Hospitalized HH
bysort renum: egen hospitalized_hh = max(hospitalized)

* Keep only those over 18 in Sep2020 (eligible for KP)
keep if age >=18

* Obsnum by group
bysort ind_code26: gen num_ind26 = _n
bysort ind_code26 java: gen num_ind26_java = _n

* Combine leaveout with regular means
replace d_employed26_java = d_employedl26_java if panelobs ==1
replace d_tot_adj_cons26_java = d_tot_adj_consl26_java if panelobs ==1
replace d_l_tot_adj_cons26_java = d_l_tot_adj_consl26_java if panelobs ==1

* Creating shock-dummies (unweighted)
rename d_employed26_java d_emp26_java
rename d_tot_adj_cons26_java d_cons26_java
rename d_l_tot_adj_cons26_java d_lcons26_java

foreach var of varlist d_emp26_java d_cons26_java {
  summ `var' if num_ind26_java ==1, d
  gen `var'_dummy = `var' >=r(p75)
}

* Labelling 
label var ltot_adj_cons "Log Consumption per Capita"
label var employed "Employed"
label var hospitalized_hh "Hospitalized Last Yr (HH)"
label var d_cons26_java_dummy "Negative Consumption Shock"
label var d_cons26_java "Industry Avg Cons. (Mar-Sep)"
label var use_internet "Use Internet"
label var socialprogram "Receive Other Social Program"
/*----------------------------------------------------*/
* Make Table
/*----------------------------------------------------*/
 * Loop over dummy and continuous shock var.
foreach dummy in "" "_dummy" {
  global varlist = "${baseline_vars26`dummy'}"
  local j = 1
  foreach var of global varlist {

    ** Set control variables, SE clustering **
    local othervars = subinstr("$varlist", "`var'", "", .)

    local vce = "vce(cluster ind_code26#java)"

    forvalues c = 0/1 {

      if `c' == 1 {
        local controls = "use_internet socialprogram `othervars'"
      }
      else {
        local controls = ""
      }
      local i =  `c' + 1

      ** Regressions **

      * Win by 22
      eststo t`i': reghdfe ever_win_22 `var' `controls' if panelobs ==1 & num ==1 & (applier ==0 | first_apply_batch <=22) , absorb(kode_kab) `vce'
      if `j'==1 {
        local obs`i' = e(N)
        summ ever_win_22 if e(sample) ==1
        local dvmean`i' = r(mean)
      }
      if `j' ==4 {
        estadd scalar obs `obs`i'' : t`i' 
        estadd scalar dvmean `dvmean`i'' : t`i' 
      } 
      local i = `i' +2
      
      * Win by 5
      eststo t`i': reghdfe ever_win_5 `var' `controls' if panelobs ==1 & num ==1 & (applier ==0 | first_apply_batch <=22 ) , absorb(kode_kab) `vce'
      if `j'==1 {
        local obs`i' = e(N)
        summ ever_win_5 if e(sample) ==1
        local dvmean`i' = r(mean)
      }
      if `j' ==4 {
        estadd scalar obs `obs`i'' : t`i' 
        estadd scalar dvmean `dvmean`i'' : t`i' 
      } 
      local i = `i' +2

      * Report Win by 5
      eststo t`i': reghdfe get_pk `var' `controls' if panelobs ==1 & num ==1 & (applier ==0 | first_apply_batch <=22) , absorb(kode_kab) `vce'
      if `j'==1 {
        local obs`i' = e(N)
        summ ever_win_5 if e(sample) ==1
        local dvmean`i' = r(mean)
      }
      if `j' ==4 {
        estadd scalar obs `obs`i'' : t`i' 
        estadd scalar dvmean `dvmean`i'' : t`i' 
      } 
      local i = `i' +2

      * Reapply
      eststo t`i': reghdfe reapplied `var' `controls' if panelobs ==1 & win_in_batch ==0 & batch < 22 , absorb(kode_kab batch) `vce'
      if `j'==1 {
        local obs`i' = e(N)
        summ reapplied if e(sample) ==1
        local dvmean`i' = r(mean)
      }
      if `j' ==4 {
        estadd scalar obs `obs`i'' : t`i' 
        estadd scalar dvmean `dvmean`i'' : t`i' 
      } 
    }
    ** Output Tables **

    if "`dummy'" == "_dummy" local tabname = "Table4"
    else local tabname = "TableA13"

    * First var + Table Top
    if `j' ==1 {
      esttab t1 t2 t3 t4 t5 t6 t7 t8 using "$KP_output/tables/descriptive/tex/`tabname'.tex", ///
      b(a2) se(a1) ///
      keep(`var') ///
      collabels(none) ///
      label ///
      mgroups("Win by Batch 22" "Win by Batch 5" "Report Win" "Reapply", pattern(1 0 1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
      star(* 0.10 ** 0.05 *** 0.01) /// 
      nomtitles ///
      noobs ///
      prehead(`"\hline \hline"') posthead("\hline \\") ///
      prefoot("") postfoot("\\") ///
      fragment ///
      nogaps ///
      replace
    }
    * Middle Vars
    else if inrange(`j', 2, 3) {
      esttab t1 t2 t3 t4 t5 t6 t7 t8 using "$KP_output/tables/descriptive/tex/`tabname'.tex", ///
      b(a2) se(a2) ///
      keep(`var') ///
      collabels(none) ///
      label ///
      star(* 0.10 ** 0.05 *** 0.01) /// 
      nomtitles nonum ///
      noobs ///
      prehead("") posthead("") ///
      prefoot("") postfoot("\\") ///
      fragment ///
      nogaps ///
      append
    }
    * Last var + Table Bottom
    else {
      esttab t1 t2 t3 t4 t5 t6 t7 t8 using "$KP_output/tables/descriptive/tex/`tabname'.tex", ///
      b(a2) se(a2) ///
      keep(`var' use_internet socialprogram) ///
      collabels(none) ///
      label ///
      star(* 0.10 ** 0.05 *** 0.01) /// 
      nomtitles nonum ///
      noobs stats(obs dvmean, label("Observations" "DV Mean") fmt(%9.0fc a2)) ///
      prehead("") posthead("") ///
      prefoot("\\ ") ///
      fragment ///
      gaps ///
      append
    }
    local j = `j' + 1
  }
}